package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YsylbBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShouYuanLieBiaoActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private EditText edit;
    private String orderid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_yuan_lie_biao);
        orderid = getIntent().getStringExtra("orderid");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("验收员列表");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        edit = (EditText) findViewById(R.id.edit);
        edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (edit.getText().toString().trim().equals("")) {
                        MyToast("请输入");
                        return false;
                    }
                    onUpload(edit.getText().toString());
                    return true;
                }
                return false;
            }
        });
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        onUpload("");
    }

    private void onUpload(String keywords) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("keywords", keywords);
        showDialog();
        XUtil.Post(Url.DECORATE_CHECKERLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YsylbBean ysylbBean = gson.fromJson(result, YsylbBean.class);
                        List<YsylbBean.InfoEntity> info1 = ysylbBean.getInfo();
                        setData(info1);
                        recycler.setVisibility(View.VISIBLE);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        recycler.setVisibility(View.GONE);
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void setData(final List<YsylbBean.InfoEntity> info1) {

        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_chanpin_yuan;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.tou), info1.get(position).getHeadimg());
                holder.setTextView(R.id.name, info1.get(position).getRealname());
                holder.setTextView(R.id.company, info1.get(position).getCompany());
                holder.setTextView(R.id.yanshoumingzi, info1.get(position).getLevel());
                holder.setTextView(R.id.sj, "手机号：" + info1.get(position).getTelephone());

                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialogv7("是否分配给" + info1.get(position).getRealname() + "?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                fenPai(info1.get(position).getUserid());
                            }
                        });
                    }
                });
            }

        });
    }

    private void fenPai(String userid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("userid", userid);
        map.put("orderid", orderid);
        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }

        XUtil.Post(Url.DECORATE_TRANSFERCHECKEDORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
